﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniversalDataAccess.BusinessEntities;

namespace UniversalDataAccess
{
    public class BuildingDataAccess : LocationsDataAccess
    {
        public BuildingDataAccess(string dbName, string conn, bool usePredefinedMetadata)
            :base(dbName, conn, usePredefinedMetadata)
        {
        }

        public List<Building> SelectBuildingsList()
        {
            List<Location> locList = SelectLocationsList();

            List<BULDING> bldList = dataContext.BULDING.ToList();

            //Merge result
            var res = (from loc in locList
                       join bld in bldList on loc.ID equals bld.ID
                       select new Building
                       {
                           DataBaseName = dataBaseName,
                           CLASS_ID = loc.CLASS_ID,
                           EXT_ID = loc.EXT_ID,
                           ID = loc.ID,
                           NAME = loc.NAME,
                           PARENT_ID = loc.PARENT_ID,
                           SQUARE = loc.SQUARE,
                           RESPONSIBLE_PERSON_ID = loc.RESPONSIBLE_PERSON_ID,
                           ADDRESS = bld.ADDRESS,
                           FLOOR_AMOUNT = (Int32)bld.AMOUNT_FLOOR,
                           ROOM_AMOUNT = (Int32)bld.AMOUNT_ROOM
                       }).ToList();

            return res;
        }

        public Building SelectBuildingBy_Id(int Id)
        {
            try
            {
                //Select Tobject part by Id
                Location basePart = SelectLocationBy_Id(Id);

                List<BULDING> bldPart = dataContext.BULDING.ToList();

                //Merge result
                var res = (from bld in bldPart
                           where bld.ID == basePart.ID
                           select new Building
                           {
                               DataBaseName = dataBaseName,
                               CLASS_ID = basePart.CLASS_ID,
                               EXT_ID = basePart.EXT_ID,
                               ID = basePart.ID,
                               NAME = basePart.NAME,
                               PARENT_ID = basePart.PARENT_ID,
                               SQUARE = basePart.SQUARE,
                               RESPONSIBLE_PERSON_ID = basePart.RESPONSIBLE_PERSON_ID,
                               ADDRESS = bld.ADDRESS,
                               ROOM_AMOUNT = (Int32)bld.AMOUNT_ROOM,
                               FLOOR_AMOUNT = (Int32)bld.AMOUNT_FLOOR
                           }).FirstOrDefault();

                return res;
            }
            catch
            {
                throw new Exception("Error while SelectBuildingBy_Id");
            }
        }

        public void InsertBuilding(Building bld, ref int generated_Id)
        {
            try
            {
                //Insert the TObject part
                InsertLocation(bld, ref generated_Id);

                int new_Id = generated_Id;
                BULDING bldPart = new BULDING();
                bldPart.LOCATION = dataContext.LOCATION.Where(t => t.ID == new_Id).FirstOrDefault();
                bldPart.ID = new_Id;
                bldPart.ADDRESS = bld.ADDRESS;
                bldPart.AMOUNT_FLOOR = bld.FLOOR_AMOUNT;
                bldPart.AMOUNT_ROOM = bld.ROOM_AMOUNT;

                //Submit changes
                dataContext.AddToBULDING(bldPart);
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while InsertBuilding");
            }
        }

        public void UpdateBuilding(Building bld)
        {
            try
            {
                UpdateLocation(bld);

                BULDING bldPart = dataContext.BULDING.Where(t => t.ID == bld.ID).FirstOrDefault();

                bldPart.ADDRESS = bld.ADDRESS;
                bldPart.AMOUNT_FLOOR = bld.FLOOR_AMOUNT;
                bldPart.AMOUNT_ROOM = bld.ROOM_AMOUNT;

                //Submit changes
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while UpdateBuilding");
            }
        }

        public void DeleteBuilding(Building bld)
        {
            try
            {
                BULDING bldPart = dataContext.BULDING.Where(t => t.ID == bld.ID).FirstOrDefault();

                dataContext.DeleteObject(bldPart);
                dataContext.SaveChanges();


                DeleteLocation(bld);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while DeleteBuilding");
            }
        }
    }
}
